﻿using JobRecruitment.Entities;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;

namespace JobRecruitment.Models
{
    public class CompanyViewModel
    {
        private readonly JobRecruitmentContext _context;

        public CompanyViewModel()
        {

        }

        public CompanyViewModel(JobRecruitmentContext context)
        {
            _context = context ?? throw new ArgumentNullException(nameof(context));
        }

        public List<Company> GetCompanys()
        {
            var companys = _context.Companys.Include(m => m.Jobs).ToList();
            companys.ToList().ForEach(m => m.Jobs.ToList().ForEach(n => n.Company = null));
            companys.ToList().ForEach(m => m.Jobs.ToList().ForEach(n => n.City = null));
            return companys;
        }

        public Company GetCompanyById(int id)
        {
            var company = _context.Companys.Include(m => m.Jobs).First(m => m.Id == id);
            company.Jobs.ToList().ForEach(m => m.Company = null);
            company.Jobs.ToList().ForEach(m => m.City.Jobs = null);
            return company;
        }
    }
}
